package tech.yangzhijie.basic.knowledge.mianshi;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class Test10Semaphore {

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(3,false);//默认 非公平锁

        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                try {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName() + " 获取位置……");
                    TimeUnit.SECONDS.sleep(3);
                    System.out.println(Thread.currentThread().getName() + " 释放位置----");
                    System.out.println("-------------------");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    semaphore.release();
                }
            }).start();
        }
    }
}
